//分类添加
function toAddProductCategory() {
    $.ajax({
        url: contextPath + "/admin/productCategory",
        method: "post",
        data: {
            action: "toAddProductCategory"
        },
        success: function (jsonStr) {
            $("#addProductCategory").html(jsonStr);
            $("input[name=select]").removeAttr("checked");
        }
    });
}
//选择商品分类级别
function selectProductCategoryLevel(obj) {
    var level = $(obj).val();
    if (level == 1) {
        $('#productCategoryLevel1').parent().parent().hide();
        $('#productCategoryLevel2').parent().parent().hide();
        
        //清空1、2级分类内容
        var options = "<option value='-1'>" + "请选择..." + "</option>";
        $("#productCategoryLevel1").html(options);
        $("#productCategoryLevel2").html(options);
    } else if (level == 2) {
        $('#productCategoryLevel1').parent().parent().show();
        $('#productCategoryLevel2').parent().parent().hide();
        //谁hide，谁就得清空数据
        var options = "<option value='-1'>" + "请选择..." + "</option>";
        $("#productCategoryLevel2").html(options);
        //加载一级分类
        queryProductCategoryList(0,'productCategoryLevel1');
    } else {
        $('#productCategoryLevel1').parent().parent().show();
        $('#productCategoryLevel2').parent().parent().show();
        //加载一级分类
        queryProductCategoryList(0,'productCategoryLevel1');
    }
}



//查询分类
function queryProductCategoryList(parentId,selectId) {
    $.ajax({
        url: contextPath + "/admin/productCategory",
        method: "post",
        data: {
            action: "queryProductCategoryList",
            parentId: parentId
        },
        success: function (jsonStr) {
        	//将json字符串 转成 json对象
            var data = eval("(" + jsonStr + ")");
            //生成HTML代码
            var options = "<option value='-1'>" + "请选择..." + "</option>";
            for (var i = 0; i < data.length; i++) {
                var option = "<option value=" + data[i].id + ">" + data[i].name + "</option>";
                options = options + option;
            }
            $("#" + selectId).html(options);
        }
    });
}

function level1Change(obj){
	
	//判断是否要加载对应的二级分类
	var type = $('#type').val();
	//新增3级分类才需要加载二级分类
	if(type==3){
		
		var id = $(obj).val();//拿到选中的1级分类id
		if(id==-1){ // 选中了 请选择项，清空二级分类
			//生成HTML代码
            var options = "<option value='-1'>" + "请选择..." + "</option>";
            $("#productCategoryLevel2").html(options);
		} else {
			//加载二级分类
	        queryProductCategoryList(id,'productCategoryLevel2');
		}
	}
	
	
	
}

function clearSelect(id){
	var options = "<option value='-1'>" + "请选择..." + "</option>";
    $("#"+id).html(options);
}


function saveOrUpdate() {
    var id = $("#id").val();
    if (id == null || id == "") {
        addProductCategory();
    } else {
        modifyProductCategory();
    }
}

//新增
function addProductCategory() {
    var productCategoryLevel1 = $("#productCategoryLevel1").val();
    var productCategoryLevel2 = $("#productCategoryLevel2").val();
    var name = $("#name").val();
    var type = $("#type").val();
    $.ajax({
        url: contextPath + "/admin/productCategory",
        method: "post",
        data: {
            action: "addProductCategory",
            name: name,
            type: type,
            productCategoryLevel1: (productCategoryLevel1 == null || productCategoryLevel1 == "") ? 0 : productCategoryLevel1,
            productCategoryLevel2: (productCategoryLevel2 == null || productCategoryLevel2 == "") ? 0 : productCategoryLevel2
        },
        success: function (jsonStr) {
            var result = eval("(" + jsonStr + ")");
            //状态判断
            if (result.status == 1) {
                location.reload();
            }
        }
    });
}

//修改
function modifyProductCategory(){
	var id = $("#id").val();
    var productCategoryLevel1 = $("#productCategoryLevel1").val();
    var productCategoryLevel2 = $("#productCategoryLevel2").val();
    var name = $("#name").val();
    var type = $("#type").val();
    $.ajax({
        url: contextPath + "/admin/productCategory",
        method: "post",
        data: {
            action: "modifyProductCategory",
            id: id,
            name: name,
            type: type,
            productCategoryLevel1: (productCategoryLevel1 == null || productCategoryLevel1 == "") ? 0 : productCategoryLevel1,
            productCategoryLevel2: (productCategoryLevel2 == null || productCategoryLevel2 == "") ? 0 : productCategoryLevel2
        },
        success: function (jsonStr) {
            var result = eval("(" + jsonStr + ")");
            //状态判断
            if (result.status == 1) {
                window.location.reload();
            }
        }
    });
}

//修改
function toUpdateProductCategoryList(obj) {
    var id = $(obj).val();
    $.ajax({
        url: contextPath + "/admin/productCategory",
        method: "post",
        data: {
            action: "toUpdateProductCategory",
            id: id
        },
        success: function (jsonStr) {
            $("#addProductCategory").html(jsonStr);
        }
    });
}

//添加商品
function addProduct(){
	//创建FormData对象
	var fd = new FormData(document.getElementById("productAdd"));
	$.ajax({
		url: contextPath+"/admin/product?action=addProduct",
		type: "POST",
		data: fd,
		dataType: "json",
		processData: false,  // 不处理数据
		contentType: false,   // 不设置内容类型
		success:function(jsonResult){
			  showMessage(jsonResult.message);
			  if (jsonResult.status == 1) {
		            window.location.href = contextPath + "/admin/product?action=index";
			  } 
		},
		error:function(e){
		    console.log(e);
		    showMessage("错误。。。");
		}
	});
}

//修改商品
function updateProduct(){
	//创建FormData对象
	var fd = new FormData(document.getElementById("productAdd"));
	$.ajax({
		url: contextPath+"/admin/product?action=updateProduct",
		type: "POST",
		data: fd,
		dataType: "json",
		processData: false,  // 不处理数据
		contentType: false,   // 不设置内容类型
		success:function(jsonResult){
			  showMessage(jsonResult.message);
			  if (jsonResult.status == 1) {
		            window.location.href = contextPath + "/admin/product?action=index";
			  } 
		},
		error:function(e){
		    console.log(e);
		    showMessage("错误。。。");
		}
	});
	
}

function showImg(obj,imgId){
	if(obj.files.length>0){
		$('#'+imgId).attr("src", window.URL.createObjectURL(obj.files[0]));
	} else {
		$('#'+imgId).attr("src", "");
	}
}
